﻿using AutoMapper;
using DocumentFormat.OpenXml.Drawing.Charts;
using MediatR;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using MyMES.Domain.yjb;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Command.BOMguans;

namespace MyMES.Read.API.Application.Handler.BOMguans
{
    /// <summary>
    /// BOM管理
    /// </summary>
    public class GETBOMguanHandler : IRequestHandler<GetBOMguancommand, APIPaging<List<BOMguan>>>
    {
        private readonly IBaseRepository<BOMguan> baseRepository;
        private readonly IBaseRepository<BomOperate> op;
        private readonly IMapper mapper;

        public GETBOMguanHandler(IBaseRepository<BOMguan> baseRepository,IBaseRepository<BomOperate> op,IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.op = op;
            this.mapper = mapper;
        }
        public async Task<APIPaging<List<BOMguan>>> Handle(GetBOMguancommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<BOMguan>> result = new APIPaging<List<BOMguan>>();
                result.Code = APIEnum.查询成功; 
                result.Msg = "显示成功";
                string sql = "SELECT * FROM BOMguan where IsDeleted=false ORDER BY BomId  DESC ";

                var list = await baseRepository.SelectAsync(sql);
                if (request.BomId>0)
                {
                    list = list.Where(x => x.BomId == request.BomId).ToList();
                }

                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);
                list = list.Skip((request.index - 1) * request.size).Take(request.size).ToList();
                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
